package dao;

import model.Book;
import utils.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;

public class BookDao {
    public HashSet list() {
        HashSet books = new HashSet();
        try {
            String sql = "select * from book";
            PreparedStatement preparedStatement = (PreparedStatement) DbUtil.con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Book book = new Book();
                book.setBookId(resultSet.getInt("id"));
                book.setBookName(resultSet.getString("book_name"));
                book.setTypeId(resultSet.getInt("type_id"));
                book.setAuthor(resultSet.getString("author"));
                book.setPublish(resultSet.getString("publish"));
                book.setPrice(resultSet.getDouble("price"));
                book.setNumber(resultSet.getInt("number"));
                book.setStatus(resultSet.getInt("status"));
                book.setRemark(resultSet.getString("remark"));
                books.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return books;
    }

    public HashSet search(String bookName) {
        HashSet books = new HashSet();
        try {
            String sql = "select * from book where book_name like \"%?%\"";
            PreparedStatement preparedStatement = (PreparedStatement) DbUtil.con.prepareStatement(sql);
            preparedStatement.setString(1, bookName);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Book book = new Book();
                book.setBookId(resultSet.getInt("id"));
                book.setBookName(resultSet.getString("book_name"));
                book.setTypeId(resultSet.getInt("type_id"));
                book.setAuthor(resultSet.getString("author"));
                book.setPublish(resultSet.getString("publish"));
                book.setPrice(resultSet.getDouble("price"));
                book.setNumber(resultSet.getInt("number"));
                book.setStatus(resultSet.getInt("status"));
                book.setRemark(resultSet.getString("remark"));
                books.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return books;
    }
    public int update(Book book) {
        try {
            String sql = "update book set book_name=?,type_id=?,author=?,publish=?,price=?,number=?,status=?,remark=? where id=?";
            PreparedStatement preparedStatement = (PreparedStatement) DbUtil.con.prepareStatement(sql);

        }catch (Exception e){
            e.printStackTrace();
        }
        return  0;
    }
}
